#:coding:utf-8
# __author__ = 'Administrator'
#
# #用Python实现五种最常见的相似性度量
#
# """
# 相似性：
#     相似性是度量量个数据对象的相似程度的度量方法
# 两种主要的相似性度量思路(X,Y是两个数据对象)：
#     ①相似度= 1 如果 X = Y
#     ②相似度= 0 如果 X ≠ Y
# """
#
# #欧几里德距离
# from math import *
# def euclidean_distance(x,y):
#     return sqrt(sum(pow(a-b,2) for a,b in zip(x,y)))
# print euclidean_distance([0,3,4,5],[7,6,3,-1])
#
# #曼哈顿距离
# def manhattan_distance(x,y):
#     return sum(abs(a-b) for a,b in zip(x,y))
#
# print manhattan_distance([10,20,10],[10,20,20])
#
# #Minkowski距离
# from decimal import Decimal
# def nth_root(value,n_root):
#     root_value = 1/float(n_root)
#     return round(Decimal(value) ** Decimal(root_value),3)
# def Minkowski_distance(x,y,p_value):
#     return nth_root(sum(pow(abs(a-b),p_value) for a,b in zip(x,y)),p_value)
#
# print Minkowski_distance([0,3,4,5],[7,6,3,-1],3)
#
# #余弦相似度
# def square_rooted(x):
#     return round(sqrt(sum([a*a for a in x])),3)
# def cosine_similarity(x,y):
#     numerate = sum(a*b for a,b in zip(x,y))
#     denominator = square_rooted(x)*square_rooted(y)
#     return round(numerate/float(denominator),3)
#
# print cosine_similarity([3,45,7,2],[2,54,13,15])
#
#
# #Jaccard相似度
# def Jaccard_similarity(x,y):
#     intersection_cardinality = len(set.intersection(*[set(x),set(y)]))
#     union_cardinality = len(set.union(*[set(x),set(y)]))
#     return intersection_cardinality/float(union_cardinality)
# print Jaccard_similarity([0,1,2,3,4,5,6],[0,2,3,5,7,9])
#
#
#
#
#






from  math import *
from decimal import Decimal

class Similarity():
    """
        相似性：
            相似性是度量量个数据对象的相似程度的度量方法
        两种主要的相似性度量思路(X,Y是两个数据对象)：
            ①相似度= 1 如果 X = Y
            ②相似度= 0 如果 X ≠ Y
        五种相似性度量函数
    """

    #欧几里德距离
    def __init__(self):
        pass
    def euclidean_distance(self,x,y):
        """返回两个列表的欧几里德距离"""
        return sqrt(sum(pow(a-b,2) for a,b in zip(x,y)))

    #曼哈顿距离
    def manhattan_distance(self,x,y):
        """返回两个列表的曼哈顿距离"""
        return sum(abs(a-b) for a,b in zip(x,y))

    #Minkowski距离

    def Minkowski_distance(self,x,y,p_value):
        """返回两个列表的Minkowski距离"""
        return self.nth_root(sum(pow(abs(a-b),p_value) for a,b in zip(x,y)),p_value)
    def nth_root(self,value,n_root):
        """返回两个值的n_root"""
        root_value = 1/float(n_root)
        return round(Decimal(value) ** Decimal(root_value),3)


    #余弦相似度

    def cosine_similarity(self,x,y):
        """返回两个列表的余弦相似度"""
        numerate = sum(a*b for a,b in zip(x,y))
        denominator = self.square_rooted(x)*self.square_rooted(y)
        return round(numerate/float(denominator),3)
    def square_rooted(self,x):
        return round(sqrt(sum([a*a for a in x])),3)



    #Jaccard相似度
    def Jaccard_similarity(self,x,y):
        """返回两个列表的Jaccard相似度"""
        intersection_cardinality = len(set.intersection(*[set(x),set(y)]))
        union_cardinality = len(set.union(*[set(x),set(y)]))
        return intersection_cardinality/float(union_cardinality)

def main():
    m = Similarity()
    print m.euclidean_distance([0,3,4,5],[7,6,3-1])
    print m.Jaccard_similarity([0,1,2,3,4,5,6],[0,2,3,5,7,9])

if __name__ == "__main__":
    main()











































